<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<azthink@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Config as ConfigModel;

/**
 * 配置控制器
 *
 * @author 李忠<azthink@qq.com>
 * @createAt 2018-08-31 10:29:59
 *
 */
class Config extends Admin {

    /**
     * 列表页面
     */
    public function index($title = null, $group = null, $type = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = ConfigModel::lists($title, $group, $type);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('group', $group);
        $this->assign('type', $type);
        $this->assign('title', $title);
        $this->setMeta('配置管理');
        return $this->fetch();
    }

    // 新增配置
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Config');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = ConfigModel::create($data)) {
                cache('db_config_data', null);
                $this->log($data['id'], '新增配置成功');
                $this->success('新增配置成功', cookie('__forward__'));
            } else {
                $this->error('新增配置失败');
            }
        } else {
            $this->assign('info', null);
            $this->assign('config_group', config('config_group_list'));
            $this->assign('config_type', config('config_type_list'));
            $this->setMeta('新增配置');
            return $this->fetch();
        }
    }

    // 更新配置
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Config');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = ConfigModel::update($data)) {
                cache('db_config_data', null);
                $this->log($data['id'], '更新配置成功');
                $this->success('更新配置成功', cookie('__forward__'));
            } else {
                $this->error('更新配置失败');
            }

        } else {
            $info = ConfigModel::get($id);

            $this->assign('info', $info);
            $this->assign('config_group', config('config_group_list'));
            $this->assign('config_type', config('config_type_list'));
            $this->setMeta('更新配置');
            return $this->fetch('add');
        }
    }

    /**
     * 排序
     */
    public function sort($group = null) {
        if ($this->request->isPost()) {
            $ids = input('ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key => $value) {
                ConfigModel::where('id', $value)->update(['sort' => $key + 1]);
            }

            $this->success('配置排序成功！');

        } else {
            if (!isset($group)) {
                $this->error('请先选择分组查询后再点击排序');
            }

            // 获取排序的数据
            $map = array(['status', '=', 1], ['group', '=', $group]);

            $data_list = ConfigModel::where($map)->field('id,name,title')->order('sort asc')->select();
            $this->assign('data_list', $data_list);
            $this->setMeta('配置排序');
            return $this->fetch();
        }
    }

    public function group($group = 1) {

        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            if ($data && is_array($data)) {
                foreach ($data['config'] as $id => $value) {
                    ConfigModel::update(array('id' => $id, 'value' => $value));
                    cache('db_config_data', null);
                    // 记录行为
                    $this->log($id, '系统配置成功');
                }
                $this->success('系统配置编辑成功');
            } else {
                $this->error('系统配置编辑失败');
            }
        } else {


            $data_list = ConfigModel::listByGroup($group);

            $this->assign('data_list', $data_list);

            $this->assign('config_group', config('config_group_list'));
            $this->assign('group', $group);

            $this->setMeta('系统配置');
            return $this->fetch();
        }

    }

    public function resume($id) {
        if (ConfigModel::state($id, 1)) {
            $this->log($id, '启用配置成功');
            $this->success('启用配置成功');
        } else {
            $this->error('启用配置失败');
        }
    }

    public function forbid($id) {
        if (ConfigModel::state($id, 0)) {
            $this->log($id, '禁用配置成功');
            $this->success('禁用配置成功');
        } else {
            $this->error('禁用配置失败');
        }
    }


    /**
     * 删除
     * @param int $id
     */
    public function delete($id) {
        if (ConfigModel::state($id, -1)) {
            $this->log($id, '删除配置成功');
            $this->success('删除配置成功');
        } else {
            $this->error('删除配置失败');
        }
    }
}